<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Exports\TouristVisitsExport;
use App\Http\Controllers\Controller;
use App\Models\TouristVisit;
use App\Services\UsersServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Facades\Excel;

class UserLoginLogController extends AdminController
{
    //
    //列表
    public function  list(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'username' => 'string',
            'type' => 'integer',
            'name' => 'string',
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $username = $request->input('username');
        $name = $request->input('name');
        $type = $request->input('type');
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $sort = $request->input('sort', 'created_at');
        $order = $request->input('order', 'asc');

        $list = UsersServices::getInstance()->userLoginLogList($username,$name,$type,$page,$limit,$sort,$order);

        return $this->success($list);
    }
    //游客列表
    public function  touristList(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'page' => 'integer',
            'limit' => 'integer',
            'sort' => 'string',
            'order' => 'string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $sort = $request->input('sort', 'created_at');
        $order = $request->input('order', 'asc');

        $list = UsersServices::getInstance()->touristList($page,$limit,$sort,$order);

        return $this->success($list);
    }
    //添加游客信息
    public function touristAdd(Request $request){
        //获取IP地址所在地区
        $ip = $request->getClientIp();
        $addressInfo = $this->info($ip);
        $address = '';
        if($addressInfo != 'unknown'){
            $address .= $addressInfo['country'].' ';
            $address .= $addressInfo['region'].' ';
            $address .= $addressInfo['city'].' ';
        }
        //添加游客访问日志
        $touristVisit = new TouristVisit();
        $touristVisit->visit_time = now()->toDateTimeString();
        $touristVisit->visit_address = $address;
        $touristVisit->visit_ip = $ip;
        $touristVisit->save();
    }

    //导出游客列表
    public function  exportTouristList(Request $request)
    {
//        $validator = Validator::make($request->all(), [
//            'page' => 'integer',
//            'limit' => 'integer',
//            'sort' => 'string',
//            'order' => 'string',
//        ]);
//        if ($validator->fails()) {
//            return $this->fail(CodeResponse::PARAM_ILLEGAL);
//        }
//        $page = $request->input('page', 1);
//        $limit = $request->input('limit', 10);
        $sort = $request->input('sort', 'created_at');
        $order = $request->input('order', 'desc');
        $data = TouristVisit::select('visit_ip', 'visit_time', 'visit_address')->orderBy($sort,$order)->get();
        $time = now()->toDateTimeString();
        return Excel::download(new TouristVisitsExport($data), "商品游客访问日志{$time}.xlsx");

    }
    public function info($ip) {
        // $response = Http::get("http://ip.taobao.com/outGetIpInfo?ip=$ip&accessKey=alibaba-inc");
        $response = Http::get('http://ip.taobao.com/outGetIpInfo', [
            'ip' => $ip,
            'accessKey' => 'alibaba-inc',
        ]);
        $data =$response->json();

        if (isset($data) && $data['code']==0) {
            return $data['data'];
        } else {
            return 'unknown';
        }
    }
}
